/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;

import com.bayu.common.lib.CommonUtil;
import com.bayu.controller.MainController;
import com.bayu.services.dao.RejectMachiningDao;
import com.bayu.services.entity.RejectMachining;
import java.awt.Color;
import java.io.OutputStream;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author Bayu
 */
@Controller
public class MachiningGraph {
    RejectMachiningDao rejectMachiningDao;
    @Autowired
    private void setRejectMachiningDao(RejectMachiningDao rejectMachiningDao){
      this.rejectMachiningDao = rejectMachiningDao;
    }

    @RequestMapping("/machining-graph.png")
    private void machiningGraph (OutputStream stream) throws Exception{
         List maxValue= rejectMachiningDao.custom("select max(harian) from RejectMachining where date between '"+MainController.startDate+"' and '"+MainController.endDate+"'");
        List maxValue2 = rejectMachiningDao.custom("select max(kum) from RejectMachining where date between '"+MainController.startDate+"' and '"+MainController.endDate+"'");
        String strMax2 = maxValue2.get(0).toString();
        String strMax = maxValue.get(0).toString();
        System.out.println(strMax2+"-"+strMax);

        double max1 = Double.valueOf(strMax).doubleValue()+1;
        double max2 = Double.valueOf(strMax2).doubleValue()+1;

        double maxtemp;
        if(max1 > max2){
            maxtemp = max1;
        }else{
            maxtemp = max2;
        }

        double min = 0.0;
        double max = maxtemp+1;
        JFreeChart chart = getMachiningGraph(min, max);
        ChartUtilities.writeChartAsPNG(stream, chart, 1480,800);
    }
    private JFreeChart getMachiningGraph(double min,double max){
       DefaultCategoryDataset dataset1 = machiningDataset1();
       String title="REJECTION MACHINING PIPE FITTING";
        String tgl1="";
      String tgl2="";
      

      if(!MainController.startDate.equals("")){
          tgl1 = CommonUtil.formatStringDate(MainController.startDate);
          tgl2 = CommonUtil.formatStringDate(MainController.endDate);
          title = title+"\r\n"+ tgl1+"-"+tgl2;
      }
       final JFreeChart chart = ChartFactory.createBarChart3D(
            title,        // chart title
           "Date",               // domain axis label
           "(% Reject)",                  // range axis label
            dataset1,                 // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips?
            false                     // URL generator?  Not required...
        );
        chart.setBackgroundPaint(new Color(255, 255, 255));
        CategoryPlot plot = chart.getCategoryPlot();

        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

        CategoryItemRenderer renderer1 = plot.getRenderer();
        renderer1.setSeriesPaint(0, new Color(153,51,102));
        renderer1.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER));
        renderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer1.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        renderer1.setBaseItemLabelsVisible(true);

        DefaultCategoryDataset dataset2 = machiningDataset2();


        plot.setDataset(1,dataset2);
        CategoryItemRenderer renderer2 = new LineAndShapeRenderer();

        renderer2.setSeriesPaint(0, Color.blue);
        renderer2.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer2.setBaseItemLabelsVisible(true);
        plot.setRenderer(1, renderer2);

        plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

        NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
        //Set Minimum dan Maximum Value Axis
        rangeAxis.setRange(min, max);

        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

       return chart;
    }


    public DefaultCategoryDataset machiningDataset1(){
        String rowValue="Rej. Harian";
        DefaultCategoryDataset data1 = new DefaultCategoryDataset();
        List data = this.rejectMachiningDao.getAllByDate(MainController.startDate, MainController.endDate);
        //List data = this.rejectMachiningDao.getAllByDate("2010-12-01", "2010-12-31");
        for(int i=0;i<data.size();i++){
          RejectMachining dt = (RejectMachining)data.get(i);
          String cat[] = dt.getDate().toString().split("-");
          data1.addValue(dt.getHarian(), rowValue, cat[2]);

          System.out.println("Data Harian "+dt.getHarian()+"-"+cat[2]);
        }

        return data1;
    }

    public DefaultCategoryDataset machiningDataset2(){
        String rowValue="Rej. Kum";
        DefaultCategoryDataset data2 = new DefaultCategoryDataset();
        List data = this.rejectMachiningDao.getAllByDate(MainController.startDate, MainController.endDate);
        //List data = this.rejectMachiningDao.getAllByDate("2010-12-01", "2010-12-31");
        for(int i=0;i<data.size();i++){
          RejectMachining dt = (RejectMachining)data.get(i);
          String cat[] = dt.getDate().toString().split("-");
          data2.addValue(dt.getKum(), rowValue, cat[2]);
          System.out.println("Data Kum "+dt.getKum()+"-"+cat[2]);
        }

        return data2;
    }

    
}
